package com.yupi.shortchain.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.core.toolkit.support.BiIntFunction;
import com.yupi.shortchain.annotation.AuthCheck;
import com.yupi.shortchain.common.BaseResponse;
import com.yupi.shortchain.common.DeleteRequest;
import com.yupi.shortchain.common.ErrorCode;
import com.yupi.shortchain.common.ResultUtils;
import com.yupi.shortchain.constant.UserConstant;
import com.yupi.shortchain.exception.BusinessException;
import com.yupi.shortchain.model.dto.urlrelate.UrlRelateAddRequest;
import com.yupi.shortchain.model.dto.urlrelate.UrlRelateUpdateRequest;
import com.yupi.shortchain.model.entity.UrlRelate;
import com.yupi.shortchain.model.vo.UrlRelateVO;
import com.yupi.shortchain.service.UrlRelateService;
import com.yupi.shortchain.service.UserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.stylesheets.LinkStyle;

import javax.annotation.Resource;
import java.util.List;

/**
 * 短链接口
 */
@RestController
@RequestMapping("/short_link")
@Slf4j
public class ShortLinkController {

    @Resource
    private UrlRelateService urlRelateService;

    @Resource
    private UserService userService;

    /**
     * 增加长链
     * todo 耗时 21s 需优化
     * @param urlRelateAddRequest
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addUrlRelate(@RequestBody UrlRelateAddRequest
                                           urlRelateAddRequest) {
        if (urlRelateAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long urlRelateId = urlRelateService.addUrlRelate(urlRelateAddRequest);
        return ResultUtils.success(urlRelateId);
    }

    /**
     * 根据 id 获取
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    @ApiOperation(value = "根据 id 获取")
    public BaseResponse<UrlRelate> getUrlRelateVoById(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UrlRelate urlRelate = urlRelateService.getById(id);
        if (urlRelate == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(urlRelate);
    }
    /**
     *删除 -- 逻辑删除
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteUrlRelate(@RequestBody DeleteRequest deleteRequest){
        if(deleteRequest == null || deleteRequest.getId() <=0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = urlRelateService.deleteUrlRelate(deleteRequest);
        if(!res ){
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(true);
    }

    /**
     * 更新  管理员权限
     * @param urlRelateUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateUrlRelate(@RequestBody UrlRelateUpdateRequest urlRelateUpdateRequest){
        if(urlRelateUpdateRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean updated = urlRelateService.updateUrlRelate(urlRelateUpdateRequest);
        if(!updated){
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(true);
    }

    /**
     *根据短链获取长链
     * @param shortChain
     * @return
     */
    @GetMapping("/getByShort")
    public BaseResponse<UrlRelateVO> getLongChain(String shortChain){
        if (CharSequenceUtil.isBlank(shortChain)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UrlRelate urlRelate = urlRelateService.getLongLink(shortChain);
        if(urlRelate == null){
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        UrlRelateVO urlRelateVO = new UrlRelateVO();
        BeanUtil.copyProperties(urlRelate,urlRelateVO);
        return ResultUtils.success(urlRelateVO);
    }



    //public BaseResponse<List<UrlRelateVO>> getListShortChain()
}
